package org.zjt.book

/**
  * PS : 无var/val的属性被当做局部变量，不能被外部调用
  */
package ExtendsTest {



  // TODO: class的属性必须声明时候加上var/val，要不然不能被调用。 无var/val被当做局部变量，
  class Person(var name :String ,var age :Int ,var work : String = "eat"){

    def desc = printf("name :%s , age :%s ,work : %s",name,age,work)

  }

  class coder( name :String , age :Int ,live : String = "codding") extends Person(name,age){

    override def desc: Unit = printf("name :%s , age :%s ,  work : %s , live : %s",name,age, work ,live )

  }

  class mysterious( name :String , age :Int ,live : String = "codding") extends Person(name,age){
    override def desc: Unit = ()
  }


  /**
    * 抽象方法、抽象属性 的学习
    * @param name
    */
  abstract class abstractPerson(var name :String){
    var id :Int
    def desc : String
  }

  // TODO: id、desc都是继承的覆盖
  class Engineer(name: String,age :Int) extends abstractPerson(name){
    override var id = age
    override def desc: String = {printf("name:%s",name);name}

    // TODO: 重写equals方法
    override def equals(obj: scala.Any): Boolean = {
      var flag :Boolean = false
      if( obj.isInstanceOf[Engineer] ){
        var var1 = obj.asInstanceOf[Engineer]
        if (var1.name == this.name && var1.id == this.id)
          flag = true
      }
      flag
    }



  }
  
  


  object main extends App{
    var coder = new coder("zhang",23)
    coder.desc


    // TODO:  _   1、可以匹配任何类型   2、可以表示忽略的对象
    coder match {
      case _:Person => println("\ncoder father is person")   //  _:Person 匹配对象的类是Person的对象，并忽略该对象
      case _ => println("\ncoder undefined")                 //  _  匹配所有的条件
    }


    // TODO: asInstanceOf coder强制类型转化为Person的实例
    var coder2 = coder.asInstanceOf[Person]
    coder2.desc


    // TODO: isInstanceOf coder是否是Person的实例
    if (coder.isInstanceOf[Person])
      coder.asInstanceOf[Person].desc

    // TODO: 得到对象的Class类
    println(classOf[coder])



    // TODO:  神秘人desc
    var mysterious = new mysterious("zhang",23)
    mysterious.desc


    // TODO: 抽象方法、抽象属性的实现
    var engineer = new Engineer("zhang",24)
    engineer.desc
    var engineer2 = new Engineer("zhang",24)
    engineer2.desc

    println("\n测试重写equals："+engineer.equals(engineer2))
  }
}
